package org.example;

import lombok.SneakyThrows;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: guochuang
 * @date: 2025-03-13 17:18
 **/
public class ConverEntity {


    @SneakyThrows
    public static Object toEntity(List<ColumnMetadata> columns, Serializable[] data) {
        Map<String, Object> obj = new HashMap<>(columns.size());
        for (int i = 0; i < data.length; i++) {
            ColumnMetadata column = columns.get(i);
            Serializable fieldValue = data[i];
            if (fieldValue instanceof Date) {
                if (fieldValue != null) {
                    data[i] = new Date(((Date) fieldValue).getTime() + 0);
                }
            } else if (fieldValue instanceof byte[]) {
                if (fieldValue != null) {
                    data[i] = new String((byte[]) fieldValue, StandardCharsets.UTF_8);
                }
            } else if (fieldValue instanceof BitSet) {
                if (fieldValue != null) {
                    data[i] = !((BitSet) fieldValue).isEmpty();
                }
            }
            obj.put(column.getColumnName(), data[i]);
        }
        return obj;
    }
}
